package com.situ.mall.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.situ.mall.common.exception.SituException;
import com.situ.mall.pojo.entity.user.User;
import com.situ.mall.user.mapper.UserMapper;
import com.situ.mall.user.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Gao
 * @since 2024-10-14
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", user.getName());
        User loginUser = userMapper.selectOne(queryWrapper);
        //判断用户名是否存在
        if (loginUser == null) {
            throw new SituException("用户名已经存在");
        }
        //判断密码是否正确
        if (!loginUser.getPassword().equalsIgnoreCase(user.getPassword())) {
            throw new SituException("密码不正确");
        }

        return loginUser;
    }

    @Override
    public void updateBalance(Long userId, BigDecimal balance) {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("balance", balance);
        updateWrapper.eq("id", userId);
        userMapper.update(updateWrapper);
    }

    @Override
    public void update(User user) {
        userMapper.updateById(user);
    }

    @Override
    public void reCharge(User user) {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("balance", user.getBalance());
        updateWrapper.eq("id", user.getId());
        userMapper.update(updateWrapper);
    }
}
